var baseURL = "../";
var type=null;
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'financial/selectFinancial',
        datatype: "json",
        colModel: [
            {label: '日期', name: 'dateTime', width: "100px", align: "center", height: "50px",index:'日期'},
            {label: '姓名', name: 'fullName', align: "center", width: "60px",index:'姓名'},
            {label: '收支', name: 'type', align: "center", width: "45px",index:'收入或支出'},//sortable:false,
            {label: '金额', name: 'money', align: "center", width: "80px",index:'金额'},
            {label: '实时余额', align: "center", name: 'realTimeBalance', width: "80px",index:'实时余额'},
            {label: '项目', align: "center", name: 'project', width: "45px",index:'项目'},
            {label: '备注', align: "center", name: 'remarks', width: "100px",index:'备注'}/*,
            { label: '状态', name: 'status',sortable:false, width: 60, formatter: function(value, options, row){
                    return value === 0 ?
                        '<span class="label label-danger">禁用</span>' :
                        '<span class="label label-success">正常</span>';
                }},
            { label: '创建时间', name: 'createTime', index: "create_time", width: 85}*/
        ],
        viewrecords: true,
        height: "100%",
        width: "100%",
        rowNum: 10,
        sortable:true,
        sortname: "日期",
        sortorder: "desc",
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        checkbox: false,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    $('.Typeshow').click(function () {
        var $t = $(this);
        if ($t.find("i").hasClass('fa-caret-up')) {
            $('.downlist').hide();
            $t.find("i").removeClass("fa-caret-up").addClass('fa-caret-down');

        } else {
            $(".Typeshow").removeClass('up');
            $('.downlist').hide();
            $t.addClass('up');
            $('.downlist').eq($(".Typeshow").index($(this)[0])).show();
            $(".fa").removeClass("fa-caret-up").addClass('fa-caret-down')
            $t.find("i").removeClass("fa-caret-down").addClass('fa-caret-up');

            //清除下拉选项中所有的选中class并重新添加
            $(".xm ul li a").removeClass('selected');
            $(".jy ul li a").removeClass('selected');
            $(".dq ul li a").removeClass('selected');
            $(".xm ul li a:contains('" + $('#xm span').text() + "')").addClass('selected');
            $(".jy ul li a:contains('" + $('#jy span').text() + "')").addClass('selected');
            $(".dq ul li a:contains('" + $('#dq span').text() + "')").addClass('selected');

            //下拉个数多的情况
            var Listlength = $(".downlist");
            for (var i = 0; i < Listlength.length; i++) {
                if ($(Listlength[i]).children().length > 8) {
                    $(Listlength[i]).css("height", "460px")
                }

            }
        }
    });
    $('#startDate').bind('change', function () {
        //获取input 元素,并实时监听用户输入
        //逻辑
        var startTime=$("#startDate").val()
        var endDate=$("#endDate").val()
        var page = $("#jqGrid").jqGrid('getGridParam', 'page');
        $("#jqGrid").jqGrid('setGridParam', {
            postData: {'type': type,'startTime': startTime,'endDate': endDate},
            page: page
        }).trigger("reloadGrid");
    });
    $('#endDate').bind('change', function () {
        //获取input 元素,并实时监听用户输入
        //逻辑
        var startTime=$("#startDate").val();
        var endTime=$("#endDate").val();
        var page = $("#jqGrid").jqGrid('getGridParam', 'page');
        $("#jqGrid").jqGrid('setGridParam', {
            postData: {'type': type,'startTime': startTime,'endTime': endTime},
            page: page
        }).trigger("reloadGrid");
    });
});


function UpDataList(thisVal) {
    UpClassCss(thisVal);
    //获取当前元素本身this
    var $t = $(thisVal);
    //获取自定义id和name的值
    var id = $t.attr("data-id");
    var name = $t.attr("data-name");
    if (id == '全部') {
        id = null;
    }
    type=id;
    var startTime=$("#startDate").val()
    var endDate=$("#endDate").val()
    var page = $("#jqGrid").jqGrid('getGridParam', 'page');
    $("#jqGrid").jqGrid('setGridParam', {
        postData: {'type': id,'startTime': startTime,'endDate': endDate},
        page: page
    }).trigger("reloadGrid");
    //alert(id+"|"+name);

}

function UpClassCss(thisVal) {
    var $t = $(thisVal);
    var name = $t.attr("data-name");
    //所有的下拉隐藏
    $('.downlist').hide();
    //获取上级元素的class
    var tabId = $t.parent().parent().attr("class");
    //下拉框文字改变
    $("#" + tabId + " span").text(name);
    $(".fa").removeClass("fa-caret-up").addClass('fa-caret-down')
}